<?php

namespace app\api\logic;

use think\Db;

class GetTimeOrderPrice
{
    /**
     * 获取金额
     * @param $orderSubscribe
     * @return float|int
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getPrice($orderSubscribe){
        if(!is_array($orderSubscribe)){
            $orderSubscribe = Db::name('order_subscribe')->where('orderSubscribeId',$orderSubscribe)->find();
        }
        $users = Db::name('user')->where('userId',$orderSubscribe['userId'])->find();
        $bili = 100;
        $isHy = false;
        if($users['gradeId']>0){
            $grade = Db::name('user_grade')->where('gradeIsDel',0)
                ->where('gradeId',$users['gradeId'])
                ->find();
            if(!empty($grade)){
                $bili = $grade['reservationDiscount'];
            }
            $isHy = true;
        }
//        echo "<pre>";
//        print_r($orderSubscribe);
//        exit;
        $position = Db::name('position')->where('positionNo', '=', $orderSubscribe['orderPositionId'])->find();
//        echo "<pre>";
//        print_r($position);
//        exit;
        $startTime = $orderSubscribe['orderStartTime'];
        $timePeriodCheckAm = array(1,2,3);
        $timePeriodCheckPm = array(4,5,6,7,8);
        $timePeriodCheckNight = array(9,10,11);
        $orderPrice = 0;
        $timePeriod = config('timePeriod');
        $timePeriod = array_combine(array_column($timePeriod, 'startTime'),$timePeriod);
        if(isset($timePeriod[$startTime])){
            $timePeriodType = $timePeriod[$startTime]['timePeriodType'];
        }else{
            $timePeriodType = 1;
        }
        if ($orderPrice === 0) {
            if ((date('w', strtotime($startTime)) == 6) || (date('w', strtotime($startTime)) == 0)){
                if (in_array($timePeriodType, $timePeriodCheckAm)) {
                    if($isHy){
                        $orderPrice = $position['memberWeekendPriceAm'];
                    }else{
                        $orderPrice = $position['weekendPriceAm'];
                    }
                } elseif (in_array($timePeriodType, $timePeriodCheckPm)) {
//                    echo "<pre>";
//                    print_r($position);
//                    exit;
                    if($isHy){
                        $orderPrice = $position['memberWeekendPricePm'];
                    }else{
                        $orderPrice = $position['weekendPricePm'];
                    }
                } elseif (in_array($timePeriodType, $timePeriodCheckNight)) {
                    if($isHy){
                        $orderPrice = $position['memberWeekendPriceNight'];
                    }else{
                        $orderPrice = $position['weekendPriceNight'];
                    }
                }
            }else{
                //不是周末
                if (in_array($timePeriodType, $timePeriodCheckAm)) {
                    if($isHy){
                        $orderPrice = $position['memberPeacetimePriceAm'];
                    }else{
                        $orderPrice = $position['peacetimePriceAm'];
                    }
                } elseif (in_array($timePeriodType, $timePeriodCheckPm)) {
                    if($isHy){
                        $orderPrice = $position['memberPeacetimePricePm'];
                    }else{
                        $orderPrice = $position['peacetimePricePm'];
                    }
                } elseif (in_array($timePeriodType, $timePeriodCheckNight)) {
                    if($isHy){
                        $orderPrice = $position['memberPeacetimePriceNight'];
                    }else{
                        $orderPrice = $position['peacetimePriceNight'];
                    }
                }
            }
        }
        $orderPrice = $orderPrice;
//        echo "<pre>";
//        print_r($orderPrice);
//        exit;
        return $orderPrice;
    }
}